package day1202friut;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Scanner;

public class FruitTest {
	static Scanner sc = new Scanner(System.in);

	ArrayList<Billmodel1> list1 = new ArrayList<>();// 存流水账的

	public static void main(String[] args) {
		new FruitTest().start();
	}

	public void start() {
		while (true) {
			System.out.println("---请选择功能----");
			System.out.println("----0-添加初始三种水果（无需选择）--");
			System.out.println("----1-添加进货单--");
			System.out.println("----2-添加水果种类--");
			System.out.println("----3-显示当前各种水果库存数--");
			System.out.println("----4-查询全部销售记录--");
			System.out.println("----5-查询指定日期间隔的销售纯利润--");
			System.out.println("----6-按销售数量显示前三名--");
			System.out.println("----7-显示水果累计销售数量/金额--");
			System.out.println("----8-显示水果累计进货数量/金额--");
			System.out.println("----9-卖出水果--");
			switch (Integer.parseInt(sc.nextLine())) {
			case 0:
				initialize();
				break;
			case 1:
				addPurchaseOrder();
				break;
			case 2:
				addFruit();
				break;
			case 3:
				findInventory();
				break;
			case 4:
				findAllDate();
				break;
			case 5:
				findsalary();
				break;
			case 6:
				print();
				break;
			case 7:
				printAll();
				break;
			case 8:
				printin();
				break;
			case 9:
				saleFruit();
				break;
			default:
				System.out.println("输入指令有误!!!");
			}
		}

	}

	// 查询指定日期的销售利润
	public void findsalary() {
		System.out.println("请按照xxxx-xx-xx xx:xx:xx的格式输入两个时间");
		System.out.println("请输入查询开始日期");
		String stime = sc.nextLine();
		System.out.println("请输入查询结束日期");
		String etime = sc.nextLine();
		ArrayList<Billmodel1> list = new BillDao().select(stime, etime);
		double sum = 0;// 用来存储纯利润
		boolean flag = false;
		for (Billmodel1 bill : list) {
				flag = true;
				sum += bill.getTotalprice() - bill.getSoldnumber() * bill.getCostprice();
		}
		if (!flag) {
			System.out.println("该时间内没有销售记录");
			return;
		}
		System.out.println("净利润： " + sum);

	}

	// 显示各种水果的累计进货数量和进货花费
	public void printin() {
		ArrayList<FruitModel1> list = new FruitDao().select(null);
		for (FruitModel1 f : list) {
			System.out.println(f.getName() + " 累计进货数量： " + f.getTotalcount() + " 累计进货金额：" + f.getInprice());
		}
	}

	// 显示全部水果的累计销售数量/金额
	public void printAll() {
		ArrayList<FruitModel1> list = new FruitDao().select(null);
		for (FruitModel1 f : list) {
			System.out.println(f.getName() + " 累计销售数量： " + f.getSoldnumber() + " 累计销售金额：" + f.getTotalprice());
		}

	}

	// 查找全部销售记录
	public void findAllDate() {
		ArrayList<Billmodel1> list = new BillDao().select(null, null);
		for (Billmodel1 bill : list) {
			System.out.println(bill.toString());
		}
	}

	// 获取一个字符串类型时间
	public String getTime() {
		Date date = new Date();
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = ft.format(date);
		return str;
	}

	// 按销售数量输出前三名
	public void print() {
		int count = 0;
		ArrayList<FruitModel1> list = new FruitDao().select(null);
		for (FruitModel1 f : list) {
			count++;
			System.out.println(f.getName() + " 累计销售数量： " + f.getSoldnumber());
			if (count == 3)
				break;
		}

	}

	// 卖出水果
	public void saleFruit() {
		System.out.println("请输入要卖出水果的名字");
		String name = sc.nextLine();
		ArrayList<FruitModel1> list = new FruitDao().select(name);
		boolean flag = true;

		if (!list.isEmpty()) {
			System.out.println("该水果库存为： " + list.get(0).getInventory());
			flag = true;
		}

		if (!flag) {
			System.out.println("没有这种水果");
			return;
		}
		System.out.println("请输入要卖出水果的数量");
		int number = sc.nextInt();
		sc.nextLine();
		if (list.get(0).getInventory() < number) {
			System.out.println("库存不足");
			return;
		}
		String time = getTime();
		int first = 0;
		if (time.substring(11).compareTo("17:00:00") > 0) {
			first = 1;
			System.out.println("当前时间  " + time);
			System.out.println("已到半价时间");
		}
		System.out.println("是否为女性顾客：输入y/n");
		String is = sc.nextLine();

		FruitModel1 fruit = list.get(0);

		if (first == 1) {
			fruit.halfprice();
		}
		if (is.equalsIgnoreCase("y"))
			fruit.femalprice();
		System.out.println("单价为: " + fruit.getSaleprice());
		System.out.println("总价为: " + fruit.getSaleprice() * number);
		System.out.println("卖出成功");
		fruit.subtractInventory(number);
		fruit.addSoldNumber(number);
		fruit.addTotalPrice(fruit.getSaleprice() * number);

		Billmodel1 bill = new Billmodel1();
		bill.setName(name);
		bill.setSoldnumber(number);
		bill.setTotalprice(fruit.getSaleprice() * number);
		bill.setSoldtime(getTime());
		bill.setCostprice(fruit.getCostprice());
		list1.add(bill);
		if (is.equalsIgnoreCase("y"))
			fruit.returnprice();
		if (first == 1)
			fruit.doubleprice();
		new FruitDao().update(fruit);
		new BillDao().insert(bill);
	}

	// 显示目前所有水果库存数
	public void findInventory() {
		System.out.println("下列为所有水果目前库存：");
		ArrayList<FruitModel1> list = new FruitDao().select(null);
		for (FruitModel1 f : list) {
			System.out.println(f.getName() + " 目前库存： " + f.getInventory());
		}
	}

	// 初始添加三种水果
	public void initialize() {
		FruitModel1 fruit1 = new FruitModel1("apple", 2.0, 1.0);
		FruitModel1 fruit2 = new FruitModel1("banana", 7.0, 4.0);
		FruitModel1 fruit3 = new FruitModel1("orange", 3.0, 1.5);
		new FruitDao().insert(fruit1);
		new FruitDao().insert(fruit2);
		new FruitDao().insert(fruit3);
	}

	// 添加水果种类
	public void addFruit() {
		System.out.println("请输入要添加的水果名字");
		String name = sc.nextLine();
		boolean flag = false;
		ArrayList<FruitModel1> list = new FruitDao().select(name);
		if (!list.isEmpty()) {
			flag = true;
		}
		if (flag) {
			System.out.println("已经存在这种水果，不需要重复添加");
			return;
		}
		System.out.println("请设置要卖出的价格");
		double saleprice = sc.nextDouble();
		System.out.println("请输入进货价格");
		double costprice = sc.nextDouble();
		FruitModel1 fruit = new FruitModel1(name, saleprice, costprice);
		sc.nextLine();
		new FruitDao().insert(fruit);
		System.out.println("添加成功");
	}

	// 水果摊进货
	public void addPurchaseOrder() {
		System.out.println("请输入要进货的水果");
		String name = sc.nextLine();
		ArrayList<FruitModel1> list = new FruitDao().select(name);
		if (list.isEmpty()) {
			System.out.println("暂时没有这种水果，请添加水果种类后进行购买");
		} else {
			System.out.println("请输入要进货的数量");
			int inventory = sc.nextInt();
			sc.nextLine();
			list = new FruitDao().select(name);
			FruitModel1 fruit = list.get(0);
			System.out.println("当前进货价格为：" + fruit.getCostprice());
			System.out.println("进货总价格为 " + inventory * fruit.getCostprice());
			fruit.addInprice(inventory * fruit.getCostprice());
			fruit.addInventory(inventory);
			fruit.addTotalCount(inventory);
			System.out.println("进货成功");
			// 下一步，更新当前水果信息；
			new FruitDao().update(fruit);
		}
	}

}
